# -*- coding: utf-8 -*-
"""
Created on Wed Mar 12 17:30:05 2025

@author: jingla
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.signal import savgol_filter
from AuxiliaryFunctions import plot_scope_signal_final
font = {'family' : 'DejaVu Sans',
        'weight' : 'normal',
        'size'   : '15'}
plt.rc('font', **font)
path = ".\\DataRectifier2\\"

#%% IVs rectifier
import matplotlib.lines as mlines
filename = ['M151_VPT0112_Ch3_IV_I+I-_Bz-22Oe_1p7K_FixedHolder_After1T.dat',
            'M172_VPT0112_Ch3_IV_I+I-_Bz-m20Oe_1p7K_FixedHolder_Afterm1T.dat']

#fig, ax1 = plt.subplots(figsize=(4.5, 3.5)) # 
fig, ax1 = plt.subplots(figsize=(4, 3.75))
for i, f in enumerate(filename):
    Vin_V, Is_mA, Vs_mV, dIs_V,	time = np.loadtxt(path+f, skiprows=1).T
    filt_dV = np.abs(np.diff(Vs_mV,append=0))>50
    print((Is_mA[filt_dV]))
    if i == 0:
        plt.plot(Vs_mV[:500], Is_mA[:500], c='C{}'.format(i + 2)) # 
    else:
        plt.plot(Vs_mV[:600], Is_mA[:600], c='C{}'.format(i + 2)) #  + 2
plt.minorticks_on()
plt.xlim([-400, 400])
plt.ylim([-2.5, 2.5])
plt.tick_params(which='both', direction='in', top=True, right=True)
plt.xlabel('$V$ (mV)', fontsize=20)
plt.ylabel('$I$ (mA)', fontsize=20)
blue_line = mlines.Line2D([], [], color='C2', linestyle='-', linewidth=2, label='$B_z\\approx -$5 Oe')
orange_line = mlines.Line2D([], [], color='C3', linestyle='-', linewidth=2, label='$B_z\\approx +$5 Oe')
plt.legend(handles=[blue_line, orange_line])
plt.title('M151 and M172')
plt.savefig('IVsRectifierCh3.pdf')
plt.show()
#%%
#%% 40 kHz figure effect capacitor
B0 = 0
Vran = [-0.45, 0.45]
filename1 = 'M187_VPT0112_Ch3_I_V+V-V_I+I-Open_Vmax1.536V_1kOhm_Bz-m10Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 40160.0 Hz.dat'
filename2 = 'M187_VPT0112_Ch3_I_V+V-V_I+I-Open_Vmax1.536V_1kOhm_Bz-m10Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCir10uFfreq 40160.0 Hz.dat'
nu1, t1_s, Vsd1_V, Isd1_mA = np.loadtxt(path+filename1, skiprows=1, delimiter=',').T
t1_s = t1_s - np.min(t1_s) #-6e-6
nu2, t2_s, Vsd2_V, Isd2_mA = np.loadtxt(path+filename2, skiprows=1, delimiter=',').T
t2_s = t2_s - np.min(t2_s)
plt.figure(figsize=[4, 4])
t0 = 0
plt.subplot(211)

plt.plot(t1_s * 1e6 + t0, savgol_filter(Isd1_mA, 13, 0))
plt.plot(t2_s * 1e6 + t0, savgol_filter(Isd2_mA, 13, 0))
#plt.plot(t3_s * 1e3 + t0, Isd3_mA)
plt.xlim([0, 100])

plt.ylabel('$I_\mathrm{in}$ (mA)', fontsize=20)
plt.minorticks_on()
plt.tick_params(which='both', direction='in', top=True, right=True)
plt.subplot(212)
print('No cap Vavg={} mV'.format(np.mean(Vsd1_V*1e3)))
print('Cap Vavg={} mV'.format(np.mean(Vsd2_V*1e3)))
plt.plot(t1_s * 1e6 + t0, Vsd1_V * 1e3, label='No capacitor')
plt.plot(t2_s * 1e6 + t0, Vsd2_V * 1e3, label='$C=10\,\\mu$F')
#plt.plot(t3_s * 1e3 + t0, Vsd3_V * 1e3, label='$C=100\,\\mu$F')
plt.minorticks_on()
plt.tick_params(which='both', direction='in', top=True, right=True)
plt.xlim([0, 100])
plt.xlabel('$t$ ($\\mu$s)', fontsize=20)
plt.ylabel('$V_\mathrm{out}$ (mV)', fontsize=20)
plt.legend()
plt.savefig('Rect2CapacitorEffect40kHz.pdf')
print('Imax={}m A Vnocapmax={} V'.format(max(Isd1_mA), max(Vsd1_V)))
#%%
#%% time and R dependence rectifier 2 V 
#%% R dependence 50 Ohm
filename = 'M161_VPT0112_Ch3_I_V+V-V_I+I-50_OhmBox_Vmax1.218V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$R=50 \\Omega$', xlim=[], ylim=[], dt = 0,
                  tzero=True, l_win=1, peak_sep=40, max_ref=50e-3, max_filt=200e-3,rev=True) #0, 50
plt.savefig('M161_R_50_Ohm.pdf')
plt.show()
#%% R dependence 100 Ohm
filename = 'M162_VPT0112_Ch3_I_V+V-V_I+I-100_OhmBox_Vmax1.234V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 36.84 Hz.dat'
#plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$R=100 \\Omega$', xlim=[], ylim=[], dt = 0,
                  tzero=True, l_win=1, peak_sep=40, rev=True) #0, 50
plt.savefig('M162_R_100_Ohm.pdf')
plt.show()
#%% R dependence 200 Ohm
filename = 'M163_VPT0112_Ch3_I_V+V-V_I+I-200_OhmBox_Vmax1.234V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$R=200 \\Omega$', xlim=[], ylim=[], dt = 0,
                  tzero=True, l_win=1, peak_sep=40, rev=True) #0, 50
plt.savefig('M163_R_200_Ohm.pdf')
plt.show()
#%% R dependence 300 Ohm
filename = 'M164_VPT0112_Ch3_I_V+V-V_I+I-300_OhmBox_Vmax1.234V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$R=300 \\Omega$', xlim=[], ylim=[], dt = 0,
                  tzero=True, l_win=1, peak_sep=40, rev=True) #0, 50
plt.savefig('M164_R_300_Ohm.pdf')
plt.show()
#%% R dependence 400 Ohm
filename = 'M165_VPT0112_Ch3_I_V+V-V_I+I-400_OhmBox_Vmax1.234V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$R=400 \\Omega$', xlim=[], ylim=[], dt = 0,
                  tzero=True, l_win=1, peak_sep=40,rev=True) #0, 50
plt.savefig('M165_R_400_Ohm.pdf')
plt.show()
#%% R dependence 500 Ohm
filename = 'M166_VPT0112_Ch3_I_V+V-V_I+I-500_OhmBox_Vmax1.234V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$R=500 \\Omega$', xlim=[], ylim=[], dt = 0,
                  tzero=True, l_win=1, peak_sep=40,rev=True) #0, 50
plt.savefig('M166_R_500_Ohm.pdf')
plt.show()
#%% R dependence 600 Ohm
filename = 'M167_VPT0112_Ch3_I_V+V-V_I+I-600_OhmBox_Vmax1.234V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$R=600 \\Omega$', xlim=[], ylim=[], dt = 0,
                  tzero=True, l_win=1, peak_sep=40,rev=True) #0, 50
plt.savefig('M167_R_600_Ohm.pdf')
plt.show()
#%% R dependence 700 Ohm
filename = 'M168_VPT0112_Ch3_I_V+V-V_I+I-700_OhmBox_Vmax1.234V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$R=700 \\Omega$', xlim=[], ylim=[], dt = 0,
                  tzero=True, l_win=1, peak_sep=40,rev=True) #0, 50
plt.savefig('M168_R_700_Ohm.pdf')
plt.show()
#%% R dependence 800 Ohm
filename = 'M169_VPT0112_Ch3_I_V+V-V_I+I-800_OhmBox_Vmax1.234V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$R=800 \\Omega$', xlim=[], ylim=[], dt = 0,
                  tzero=True, l_win=1, peak_sep=40,rev=True) #0, 50
plt.savefig('M169_R_800_Ohm.pdf')
plt.show()
#%% R dependence 900 Ohm
filename = 'M170_VPT0112_Ch3_I_V+V-V_I+I-900_OhmBox_Vmax1.234V_1kOhm_Bz-p24Oe__1p7K_FixedHolder_Vampl_20x_SimplCapCirNoCapfreq 36.84 Hz.dat'
plt.figure(figsize=[3,2.5])
plot_scope_signal_final(path+filename, title='$R=900 \\Omega$', xlim=[], ylim=[], dt = 0,
                  tzero=True, l_win=1, peak_sep=40, rev=True) #0, 50
plt.savefig('M170_R_900_Ohm.pdf')
plt.show()
#nu1, t1_s, Vsd1_V, Isd1_mA = np.loadtxt(filename, skiprows=1, delimiter=',').T
#%% Output vs R second rectifier
R =    np.array([50,   100,  200,  300, 400, 500, 600, 700, 800, 900])
#Voutmax = np.array([38.6, 70.3, 94.3, 105, 114, 116, 119, 123, 123, 123])
Voutmax = np.array([49.2, 72.9, 96.1, 107.1, 114, 118, 121, 123, 123, 123])
dVoutmax = np.array([0.7,0.9,1,1,1,1,1,1,1,1])
Imax = np.array([1.688, 1.664, 1.648, 1.624, 1.624, 1.672, 1.656, 1.67, 1.67, 1.67] )
plt.errorbar(R, Voutmax/Imax, yerr=dVoutmax/Imax, fmt='o', color='k', capsize=1)
#plt.plot(R, Voutmax/Imax, 'ko')
popt,pcov = curve_fit(lambda R, Rn: R/(2*R/Rn+1), R, Voutmax/Imax)
R_long = np.linspace(0, 900, 200)
plt.plot(R_long, R_long/(2*R_long/popt[0]+1))
plt.tick_params(direction='in', top=True, right=True)
plt.xlabel('$R$ ($\\Omega$)', fontsize=20)
plt.ylabel('$R_\\mathrm{out}^\\mathrm{max}/I_\\mathrm{in}^\\mathrm{max}$ ($\\Omega$)', fontsize=20)
plt.savefig('Rdep2ndRectifier.pdf')
plt.show()
#%%
#%% Number of peaks per amplitude and resistance
R = np.array([50,100,200,300,400,500,600,700,800,900])
n1 = np.array([31,45,47,46,46,47,46,46,46,46])
plt.figure(figsize=[3,3.93])
plt.plot(R, n1,'ok', label='Low amplitude')
#plt.plot(R, n2,'or', label='High amplitude')
plt.minorticks_on()
plt.tick_params(direction='in', top=True, right=True, which='both')
plt.xlabel('$R$ ($\\Omega$)', fontsize=20)
plt.ylabel('n. peaks', fontsize=20)
plt.xticks([0, 400,800])
#plt.legend()
plt.savefig('NumberOfPoints2ndRectifier.pdf')
plt.show()